package anyVehicle;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import net.minecraft.src.ChunkLoader;
import net.minecraft.src.CompressedStreamTools;
import net.minecraft.src.IChunkLoader;
import net.minecraft.src.ISaveHandler;
import net.minecraft.src.MinecraftException;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.SaveHandler;
import net.minecraft.src.WorldInfo;
import net.minecraft.src.WorldProvider;
import net.minecraft.src.WorldProviderHell;
import net.minecraft.src.WorldSettings;

/**
 * One instance of {@link VehicleSaveHandler} per instance of
 * {@link VehicleBlocks} A vehicle data is stored in
 * <worldName>/<vechicles>/<vehicleID>
 * 
 * @author As You Wish
 * 
 */
public class VehicleSaveHandler implements ISaveHandler {

	private static File vehiclesDir;
	private static final Logger logger = Logger.getLogger("Minecraft");
	private final File saveDirectory;
	// private final File playersDirectory;
	private final long now = System.currentTimeMillis();
	private String worldName;
	private File dataDir;

	public VehicleSaveHandler(File file, String worldName, String vehicleID, boolean multiplayer) {
		saveDirectory = new File(new File(file, worldName), vehicleID);
		saveDirectory.mkdirs();
		// playersDirectory = new File(saveDirectory, "players");
		// if(multiplayer)
		// {
		// playersDirectory.mkdirs();
		// }
		dataDir = new File(saveDirectory, "data");
		this.worldName = worldName;

	}



	@Override
	public void checkSessionLock() {
		// do nothing
	}

	// @Override
	// public IChunkLoader getChunkLoader(WorldProvider worldprovider) {
	// return new ChunkLoader(vehicleDataDir, true);
	// }
	//
	// @Override
	// public WorldInfo loadWorldInfo() {
	// return new WorldInfo(new WorldSettings(0L, 0, false), worldName);
	// }

	@Override
	public File getMapFile(String s) {
		return new File(dataDir, (new StringBuilder()).append(s).append(".dat").toString());
	}

	public IChunkLoader getChunkLoader(WorldProvider worldprovider) {
		File file = new File(saveDirectory, "VDIM-1");
		file.mkdirs();
		return new ChunkLoader(file, true);
	}

	public WorldInfo loadWorldInfo() {
		File file = new File(saveDirectory, "level.dat");
		if (file.exists()) {
			try {
				NBTTagCompound nbttagcompound = CompressedStreamTools.loadGzippedCompoundFromOutputStream(new FileInputStream(file));
				NBTTagCompound nbttagcompound2 = nbttagcompound.getCompoundTag("Data");
				return new WorldInfo(nbttagcompound2);
			} catch (Exception exception) {
				exception.printStackTrace();
			}
		}
		file = new File(saveDirectory, "level.dat_old");
		if (file.exists()) {
			try {
				NBTTagCompound nbttagcompound1 = CompressedStreamTools.loadGzippedCompoundFromOutputStream(new FileInputStream(file));
				NBTTagCompound nbttagcompound3 = nbttagcompound1.getCompoundTag("Data");
				return new WorldInfo(nbttagcompound3);
			} catch (Exception exception1) {
				exception1.printStackTrace();
			}
		}
		return null;
	}

	public void saveWorldInfoAndPlayer(WorldInfo worldinfo, List list) {
		NBTTagCompound nbttagcompound = worldinfo.getNBTTagCompoundWithPlayer(list);
		NBTTagCompound nbttagcompound1 = new NBTTagCompound();
		nbttagcompound1.setTag("Data", nbttagcompound);
		try {
			File file = new File(saveDirectory, "level.dat_new");
			File file1 = new File(saveDirectory, "level.dat_old");
			File file2 = new File(saveDirectory, "level.dat");
			CompressedStreamTools.writeGzippedCompoundToOutputStream(nbttagcompound1, new FileOutputStream(file));
			if (file1.exists()) {
				file1.delete();
			}
			file2.renameTo(file1);
			if (file2.exists()) {
				file2.delete();
			}
			file.renameTo(file2);
			if (file.exists()) {
				file.delete();
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}

	public void saveWorldInfo(WorldInfo worldinfo) {
		NBTTagCompound nbttagcompound = worldinfo.getNBTTagCompound();
		NBTTagCompound nbttagcompound1 = new NBTTagCompound();
		nbttagcompound1.setTag("Data", nbttagcompound);
		try {
			File file = new File(saveDirectory, "level.dat_new");
			File file1 = new File(saveDirectory, "level.dat_old");
			File file2 = new File(saveDirectory, "level.dat");
			CompressedStreamTools.writeGzippedCompoundToOutputStream(nbttagcompound1, new FileOutputStream(file));
			if (file1.exists()) {
				file1.delete();
			}
			file2.renameTo(file1);
			if (file2.exists()) {
				file2.delete();
			}
			file.renameTo(file2);
			if (file.exists()) {
				file.delete();
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}






	@Override
	public String func_40530_d() {
		
		return worldName;
	}

}
